package com.tjetc.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.tjetc.domain.Scheduling;
import com.tjetc.service.SchedulingService;
import com.tjetc.service.impl.SchedulingServiceImpl;
import com.tjetc.vo.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * (scheduling)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/scheduling")
public class SchedulingController {
    /**
     * 服务对象
     */
    @Resource
    private SchedulingService schedulingService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Scheduling selectOne(Integer id) {
        return schedulingService.selectByPrimaryKey(id);
    }

    @GetMapping("getState")
    public Result getState(@RequestParam("cid") Integer cid) {
        try {
            return schedulingService.getState(cid);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return Result.fail("失败");
    }

    @GetMapping("getSchedulingList")
    public Result getSchedulingList(@RequestParam("cid") Integer cid, @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam("date") Date date) {
        return schedulingService.getSchedulingList(cid, date);
    }
    @GetMapping("delete")
    public Result delete(@RequestParam("id") Integer id) {
        return schedulingService.delete(id);
    }
    @PostMapping("save")
    public Result save(@RequestBody Scheduling scheduling) {
        if (scheduling.getId() == null){
            return schedulingService.add(scheduling);
        }else {
            return schedulingService.update(scheduling);
        }
    }
    @RequestMapping("selByDoc")
    public Result selByDoc(@RequestParam("did") Integer did) {
        return schedulingService.selByDoc(did);
    }

    @GetMapping("getNowScheduling")
    public Result getNowScheduling(@RequestParam("did") Integer did) {
        return schedulingService.getNowScheduling(did);
    }
}
